package com.example.auth.demo.controller;

import com.example.auth.demo.domain.PageResult;
import com.example.auth.demo.domain.ResultCode;
import com.example.auth.demo.domain.ResultJson;
import com.example.auth.demo.domain.auth.Guild;
import com.example.auth.demo.domain.auth.GuildActivity;
import com.example.auth.demo.domain.auth.GuildActivityParticipant;
import com.example.auth.demo.domain.auth.GuildChat;
import com.example.auth.demo.domain.auth.GuildContribution;
import com.example.auth.demo.domain.auth.GuildDiplomacy;
import com.example.auth.demo.domain.auth.GuildMember;
import com.example.auth.demo.domain.auth.GuildNotice;
import com.example.auth.demo.service.AuthService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(description = "公会管理相关接口")
@RequestMapping("/api/v1/guild")
public class GuildController {

    @Value("${jwt.header}")
    private String tokenHeader;

    private final AuthService authService;

    @Autowired
    public GuildController(AuthService authService) {
        this.authService = authService;
    }

    // 公会表相关接口
    @PostMapping
    @ApiOperation(value = "创建公会")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson createGuild(@RequestBody Guild guild, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.createGuild(guild);
        return ResultJson.ok();
    }

    @GetMapping("/{guildId}")
    @ApiOperation(value = "根据公会ID获取公会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<Guild> getGuildById(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        Guild guild = authService.getGuildById(guildId);
        return ResultJson.ok(guild);
    }

    @GetMapping
    @ApiOperation(value = "分页获取所有公会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    public ResultJson<PageResult<Guild>> getAllGuilds(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "id") String sortField,  // 新增排序字段参数
            @RequestParam(defaultValue = "desc") String sortOrder  // 新增排序顺序参数
    ) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }

        PageResult<Guild> guilds = authService.getGuildsByPage(page, pageSize,sortField, sortOrder);
        return ResultJson.ok(guilds);
    }

    @PutMapping
    @ApiOperation(value = "更新公会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuild(@RequestBody Guild guild, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuild(guild);
        return ResultJson.ok();
    }

    @DeleteMapping("/{guildId}")
    @ApiOperation(value = "删除公会")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteGuild(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.deleteGuild(guildId);
        return ResultJson.ok();
    }

    // 公会成员表相关接口
    @PostMapping("/member")
    @ApiOperation(value = "添加公会成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson addGuildMember(@RequestBody GuildMember member, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.addGuildMember(member);
        return ResultJson.ok();
    }

    @GetMapping("/member/{memberId}")
    @ApiOperation(value = "根据成员ID获取公会成员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<GuildMember> getGuildMemberById(@PathVariable Long memberId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        GuildMember member = authService.getGuildMemberById(memberId);
        return ResultJson.ok(member);
    }

    @GetMapping("/{guildId}/members")
    @ApiOperation(value = "根据公会ID获取公会所有成员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildMember>> getGuildMembersByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildMember> members = authService.getGuildMembersByGuildId(guildId);
        return ResultJson.ok(members);
    }

    @PutMapping("/member")
    @ApiOperation(value = "更新公会成员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuildMember(@RequestBody GuildMember member, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuildMember(member);
        return ResultJson.ok();
    }

    @DeleteMapping("/member/{memberId}")
    @ApiOperation(value = "删除公会成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteGuildMember(@PathVariable Long memberId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.deleteGuildMember(memberId);
        return ResultJson.ok();
    }

    @PutMapping("/member/{memberId}/approve")
    @ApiOperation(value = "审核通过公会成员申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson approveGuildMember(@PathVariable Long memberId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.approveGuildMember(memberId);
        return ResultJson.ok();
    }
    @GetMapping("/members/all")
    @ApiOperation(value = "获取所有公会成员信息（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query")
    })
    public ResultJson<PageResult<GuildMember>> getAllGuildMembers(
            @RequestHeader(value = "Authorization", required = false) String token,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        System.out.println("接收到的页码page: " + page);
        System.out.println("接收到的每页数量pageSize: " + pageSize);
        if (token == null) {
            token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
        }
        PageResult<GuildMember> members = authService.getAllGuildMembers(page, pageSize);
        return ResultJson.ok(members);
    }


    // 公会公告表相关接口
    @PostMapping("/notice")
    @ApiOperation(value = "发布公会公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson publishGuildNotice(@RequestBody GuildNotice notice, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.publishGuildNotice(notice);
        return ResultJson.ok();
    }

    @GetMapping("/notice/{noticeId}")
    @ApiOperation(value = "根据公告ID获取公会公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<GuildNotice> getGuildNoticeById(@PathVariable Long noticeId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        GuildNotice notice = authService.getGuildNoticeById(noticeId);
        return ResultJson.ok(notice);
    }

    @GetMapping("/{guildId}/notices")
    @ApiOperation(value = "根据公会ID获取公会所有公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildNotice>> getGuildNoticesByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildNotice> notices = authService.getGuildNoticesByGuildId(guildId);
        return ResultJson.ok(notices);
    }

    @PutMapping("/notice")
    @ApiOperation(value = "更新公会公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuildNotice(@RequestBody GuildNotice notice, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuildNotice(notice);
        return ResultJson.ok();
    }

    @DeleteMapping("/notice/{noticeId}")
    @ApiOperation(value = "删除公会公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteGuildNotice(@PathVariable Long noticeId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.deleteGuildNotice(noticeId);
        return ResultJson.ok();
    }

    @GetMapping("/notices/all")
    @ApiOperation(value = "查询所有公会公告信息（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query")
    })
    public ResultJson<PageResult<GuildNotice>> getAllGuildNotices(
            @RequestHeader(value = "Authorization", required = false) String token,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        if (token == null) {
            token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
        }
        PageResult<GuildNotice> notices = authService.getAllGuildNotices(page, pageSize);
        return ResultJson.ok(notices);
    }

    // 公会活动表相关接口
    @PostMapping("/activity")
    @ApiOperation(value = "创建公会活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson createGuildActivity(@RequestBody GuildActivity activity, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.createGuildActivity(activity);
        return ResultJson.ok();
    }

    @GetMapping("/activity/{activityId}")
    @ApiOperation(value = "根据活动ID获取公会活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<GuildActivity> getGuildActivityById(@PathVariable Long activityId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        GuildActivity activity = authService.getGuildActivityById(activityId);
        return ResultJson.ok(activity);
    }

    @GetMapping("/{guildId}/activities")
    @ApiOperation(value = "根据公会ID获取公会所有活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildActivity>> getGuildActivitiesByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildActivity> activities = authService.getGuildActivitiesByGuildId(guildId);
        return ResultJson.ok(activities);
    }

    @PutMapping("/activity")
    @ApiOperation(value = "更新公会活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuildActivity(@RequestBody GuildActivity activity, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuildActivity(activity);
        return ResultJson.ok();
    }

    @DeleteMapping("/activity/{activityId}")
    @ApiOperation(value = "删除公会活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson deleteGuildActivity(@PathVariable Long activityId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.deleteGuildActivity(activityId);
        return ResultJson.ok();
    }
    @GetMapping("/activities/all")
    @ApiOperation(value = "获取所有公会活动信息（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query")
    })
    public ResultJson<PageResult<GuildActivity>> getAllGuildActivities(
            @RequestHeader(value = "Authorization", required = false) String token,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        if (token == null) {
            token = request.getHeader(tokenHeader);
            if (token == null) {
                return ResultJson.failure(ResultCode.UNAUTHORIZED);
            }
        }
        PageResult<GuildActivity> activities = authService.getAllGuildActivities(page, pageSize);
        return ResultJson.ok(activities);
    }
    // 公会活动参与表相关接口
    @PostMapping("/activity/participant")
    @ApiOperation(value = "添加公会活动参与者")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson addActivityParticipant(@RequestBody GuildActivityParticipant participant, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.addActivityParticipant(participant);
        return ResultJson.ok();
    }

    @GetMapping("/activity/{activityId}/participants")
    @ApiOperation(value = "根据活动ID获取公会活动所有参与者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildActivityParticipant>> getParticipantsByActivityId(@PathVariable Long activityId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildActivityParticipant> participants = authService.getParticipantsByActivityId(activityId);
        return ResultJson.ok(participants);
    }

    @DeleteMapping("/activity/participant/{participantId}")
    @ApiOperation(value = "删除公会活动参与者")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson removeActivityParticipant(@PathVariable Long participantId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.removeActivityParticipant(participantId);
        return ResultJson.ok();
    }

    @GetMapping("/activity/participants/all")
    @ApiOperation(value = "获取所有公会活动参与者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildActivityParticipant>> getAllActivityParticipants(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildActivityParticipant> participants = authService.getAllActivityParticipants();
        return ResultJson.ok(participants);
    }


    // 公会贡献表相关接口
    @PostMapping("/contribution")
    @ApiOperation(value = "记录公会贡献")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson recordGuildContribution(@RequestBody GuildContribution contribution, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.recordGuildContribution(contribution);
        return ResultJson.ok();
    }

    @GetMapping("/{playerId}/contributions")
    @ApiOperation(value = "根据玩家ID获取玩家的公会贡献记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildContribution>> getContributionsByPlayerId(@PathVariable Long playerId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildContribution> contributions = authService.getContributionsByPlayerId(playerId);
        return ResultJson.ok(contributions);
    }

    @GetMapping("/{guildId}/contributions")
    @ApiOperation(value = "根据公会ID获取公会的所有贡献记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildContribution>> getContributionsByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildContribution> contributions = authService.getContributionsByGuildId(guildId);
        return ResultJson.ok(contributions);
    }

    @PutMapping("/contribution")
    @ApiOperation(value = "更新公会贡献记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuildContribution(@RequestBody GuildContribution contribution, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuildContribution(contribution);
        return ResultJson.ok();
    }

    @GetMapping("/contributions/all")
    @ApiOperation(value = "获取所有公会贡献记录（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，默认1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量，默认10", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段，默认recordTime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方向，asc/desc，默认desc", dataType = "string", paramType = "query")
    })
    public ResultJson<PageResult<GuildContribution>> getAllGuildContributions(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false, defaultValue = "recordTime") String sort,
            @RequestParam(required = false, defaultValue = "desc") String order,
            HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        PageResult<GuildContribution> pageResult = authService.getAllGuildContributions(page, pageSize, sort, order);
        return ResultJson.ok(pageResult);
    }

    // 公会外交关系表相关接口
    @PostMapping("/diplomacy")
    @ApiOperation(value = "建立公会外交关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson establishDiplomacy(@RequestBody GuildDiplomacy diplomacy, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.establishDiplomacy(diplomacy);
        return ResultJson.ok();
    }

    @GetMapping("/diplomacy/{diplomacyId}")
    @ApiOperation(value = "根据外交关系ID获取公会外交关系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<GuildDiplomacy> getGuildDiplomacyById(@PathVariable Long diplomacyId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        GuildDiplomacy diplomacy = authService.getGuildDiplomacyById(diplomacyId);
        return ResultJson.ok(diplomacy);
    }

    @GetMapping("/{guildId}/diplomacies")
    @ApiOperation(value = "根据公会ID获取公会的所有外交关系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildDiplomacy>> getDiplomaciesByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildDiplomacy> diplomacies = authService.getDiplomaciesByGuildId(guildId);
        return ResultJson.ok(diplomacies);
    }

    @PutMapping("/diplomacy")
    @ApiOperation(value = "更新公会外交关系信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson updateGuildDiplomacy(@RequestBody GuildDiplomacy diplomacy, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.updateGuildDiplomacy(diplomacy);
        return ResultJson.ok();
    }

    @DeleteMapping("/diplomacy/{diplomacyId}")
    @ApiOperation(value = "终止公会外交关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson terminateDiplomacy(@PathVariable Long diplomacyId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.terminateDiplomacy(diplomacyId);
        return ResultJson.ok();
    }


    @GetMapping("/diplomacies")
    @ApiOperation(value = "获取所有公会外交关系（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码，从1开始", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段，默认id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方式，asc/desc，默认desc", dataType = "string", paramType = "query")
    })
    public ResultJson<PageResult<GuildDiplomacy>> getAllDiplomacies(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "id") String sort,
            @RequestParam(defaultValue = "desc") String order,
            HttpServletRequest request
    ) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        PageResult<GuildDiplomacy> pageResult = authService.getAllDiplomacies(page, pageSize, sort, order);
        return ResultJson.ok(pageResult);
    }
    // 公会聊天表相关接口
    @PostMapping("/chat")
    @ApiOperation(value = "发送公会聊天消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson sendGuildChat(@RequestBody GuildChat chat, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        authService.sendGuildChat(chat);
        return ResultJson.ok();
    }

    @GetMapping("/{guildId}/chats")
    @ApiOperation(value = "根据公会ID获取公会的聊天记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")
    })
    public ResultJson<List<GuildChat>> getChatsByGuildId(@PathVariable Long guildId, HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        if (token == null) {
            return ResultJson.failure(ResultCode.UNAUTHORIZED);
        }
        List<GuildChat> chats = authService.getChatsByGuildId(guildId);
        return ResultJson.ok(chats);
    }
}